package core.sceneManager.sceneSection.scene3DSection
{
	import core.MainManager;
	import core.sceneManager.sceneSection.ISceneSection;
	import core.sceneManager.sceneSection.SceneSection;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.filters.GlowFilter;
	import flash.net.URLRequest;
	
	import memory.MemoryRecovery;
	
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	
	import structure.DictionaryAndArrayEvent;
	import structure.DictionarySingle;
	/**
	 * 三维全景中的静态热点，罗盘或罗盘指针
	 * 当所需要的图片没有在数据域里面注册的时候。那么就在数据域里面注册这个数据，并加载新的图片数据，加载完毕之后在数据域里面更新数据
	 * 当所需的图片在数据域里面已经注册，但是里面没有图片数据的时候，就在数据域里添加监听器，等待数据加载完毕再创建平面
	 * 当所需的图片已经注册且已经存在，那么直接那所需要的图片数据
	 */ 
	public class StaticPlane extends SceneSection implements ISceneSection
	{
		protected var plane:Plane;
		protected var url:String;
		protected var loader:Loader;
		protected var _waitForComplete:Boolean;
		protected static var dataStructure:DictionarySingle=new DictionarySingle(); ///保持内存唯一的数据结构
		protected var _mouseClickScript:String;                                     //鼠标点击时的脚本
		protected var isDown:Boolean=false;
		protected var _sign:Boolean=false;                                          //是否要记录当前平面是否被点击过
		protected var glowFilter:GlowFilter;                                        //给热点添加发光效果
		public function StaticPlane(target:IEventDispatcher=null)
		{
			super(target);
			plane=new Plane(new ColorMaterial(321321, 0,true),400,400);
			plane.useOwnContainer=true;
			addFilter();
		}
		private function addFilter():void                              //添加filter,鼠标滑过的时候就发光，鼠标离开就不发光
		{
			glowFilter= new GlowFilter(85, 1, 16, 16, 2, 1, false, false);
			plane.addEventListener(InteractiveScene3DEvent.OBJECT_OVER,on_mouse_over);
			plane.addEventListener(InteractiveScene3DEvent.OBJECT_OUT,on_mouse_out);
		}
		private function on_mouse_over(e:InteractiveScene3DEvent):void
		{
			plane.filters=[glowFilter];
		}
		private function on_mouse_out(e:InteractiveScene3DEvent):void
		{
			plane.filters=[];
		}
		override public function excute(xml:XML, container:*):void
		{
			this.dataBase=xml;
			for each(var item:XML in xml.attributes())
			{
				var key:String=item.name();
				try
				{
					this[key]=item.toString();
				}catch(e:Error)
				{
					try
					{
						plane[key]=item.toString();
					}catch(e:Error){}
				}
			}
			container.addChild(plane);
		}
		override public function start():void
		{
			if(url!=null)
			{
				if(dataStructure.addValue(url,{}))//如果数据结构里不存在这个数据,那么就加载新的数据
				{
					loader=new Loader();
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE,on_loaded);
					loader.load(new URLRequest(url));
				}else if(dataStructure.getContent(url)==null)
				{
					dataStructure.addEventListener(DictionaryAndArrayEvent.COMPLETE,on_shareData_loaded);
				}else
				{
					on_shareData_loaded(null);
				}
			}
		}
		protected function on_shareData_loaded(e:DictionaryAndArrayEvent):void//等待共享数据的加载完毕
		{
			if(e!=null && e.key!=url) return;
			MemoryRecovery.getInstance().gcFun(dataStructure,DictionaryAndArrayEvent.COMPLETE,on_shareData_loaded);
			setMaterial(Bitmap(dataStructure.getContent(url)).bitmapData);
		}
		protected function on_loaded(e:Event):void//新数据加载完毕
		{
			dataStructure.setContent(url,loader.content);//在数据结构里面存储content,供给其他平面使用
			setMaterial(Bitmap(loader.content).bitmapData);
		}
		//设置平面的材质,*是为了避免继承的时候不能重载
		protected function setMaterial(data:*):void
		{
			var bitmapMaterial:BitmapMaterial=new BitmapMaterial(data,true);
			plane.material=bitmapMaterial;
			bitmapMaterial.doubleSided=false;
			bitmapMaterial.smooth=true;
			bitmapMaterial.interactive=true;
			if(_waitForComplete) this.dispatchEvent(new Event(Event.COMPLETE));
		}
		override public function dispose():void
		{
			if(loader!=null) MemoryRecovery.getInstance().gcFun(loader.contentLoaderInfo,Event.COMPLETE,on_loaded);
			MemoryRecovery.getInstance().gcFun(plane,InteractiveScene3DEvent.OBJECT_PRESS,on_mouse_down);
			MemoryRecovery.getInstance().gcFun(plane,InteractiveScene3DEvent.OBJECT_RELEASE,on_mouse_up);
			MemoryRecovery.getInstance().gcObj(plane);plane=null;
			MemoryRecovery.getInstance().gcObj(loader);loader=null;
			if(dataStructure.length>0)
			{
				dataStructure.dispose();dataStructure=new DictionarySingle();
			}
		}
		//////////////////////////////////创建属性，供XML配置/////////////////////
		protected function set waitForComplete(val:int):void
		{
			if(val==1)
			{
				this._waitForComplete=true;
			}
		}
		//对于透明的面板，设置了这个属性就可以看得到它的具体形状
		private function set debuge(val:int):void
		{
			if(val==1)
			{
				plane.material=new ColorMaterial(321321, 1,true); 
				plane.material.interactive=true;
			}
		}
		//鼠标点击所要执行的脚本
		protected function set mouseClickScript(val:String):void
		{
			if(plane==null) return;
			plane.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS,on_mouse_down);
			plane.addEventListener(InteractiveScene3DEvent.OBJECT_RELEASE,on_mouse_up);
			_mouseClickScript=val;
		}
		//鼠标按下事件
		private function on_mouse_down(e:InteractiveScene3DEvent):void
		{
			isDown=true;
		}
		//鼠标起来事件
		private function on_mouse_up(e:InteractiveScene3DEvent):void
		{
			if(isDown)   //如果鼠标曾经按下过，现在鼠标起来了，肯定是已经点击了
			{
				if(_sign)//如果要记录当前对象被点击过
				{
					plane.alpha=0.5;
					this.dataBase.@alpha=0.5;
				}
				MainManager.getInstance().scriptManager.runScriptDirectly(_mouseClickScript);
			}
			isDown=false;
		}
		//设置是否要记录，需要记录的话，被点击了之后，热点会变透明
		protected function set sign(val:int):void
		{
			if(val==1)_sign=true;
		}
		//设置发光的颜色
		protected function set filterColor(val:uint):void
		{
			if(glowFilter!=null) glowFilter.color=val;
		}
	}
}